package fei.yuzhi.com.loveflowers.fragment.shopping;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import fei.yuzhi.com.loveflowers.R;

/**
 * Created by Administrator on 2016/10/19 0019.
 */
public class ShoppingAdapter extends BaseAdapter{
    private Context context;
    private  List<ShoppingBean> mlist;
    int totalCount = 0;
    double totalPrice = 0.00;
    OnGoodsCheckedChangeListener onGoodsCheckedChangeListener;
    OnAllCheckedBoxNeedChangeListener onAllCheckedBoxNeedChangeListener;

    public ShoppingAdapter(Context context, List<ShoppingBean> mlist) {
        this.context=context;
        this.mlist=mlist;
    }

    public void setOnGoodsCheckedChangeListener(OnGoodsCheckedChangeListener onGoodsCheckedChangeListener) {
        this.onGoodsCheckedChangeListener = onGoodsCheckedChangeListener;
    }
    public void setOnAllCheckedBoxNeedChangeListener(OnAllCheckedBoxNeedChangeListener onAllCheckedBoxNeedChangeListener) {
        this.onAllCheckedBoxNeedChangeListener = onAllCheckedBoxNeedChangeListener;
    }

    @Override
    public int getCount() {
        return mlist!=null?mlist.size():0;
    }

    @Override
    public Object getItem(int i) {
        return mlist!=null?mlist.get(i):null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_shopping_body,null);
            holder=new ViewHolder();
            holder.cb = (CheckBox) view.findViewById(R.id.cb_shopping_body_select);
            holder.tv_cb_text= (TextView) view.findViewById(R.id.cb_shopping_body_select_text);
            holder.iv_delete= (ImageView) view.findViewById(R.id.iv_shopping_body_delete);
            holder.iv_icon= (ImageView) view.findViewById(R.id.iv_shopping_body_icon);
            holder.tv_name= (TextView) view.findViewById(R.id.tv_shopping_body_title);
            holder.tv_press= (TextView) view.findViewById(R.id.tv_shopping_body_press);
            holder.iv_reduce= (ImageView) view.findViewById(R.id.iv_shopping_body_reduce);
            holder.tv_num= (TextView) view.findViewById(R.id.tv_shopping_body_count_now);
            holder.iv_add= (ImageView) view.findViewById(R.id.iv_shopping_body_add);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }

        final ShoppingBean shopBean=mlist.get(i);
        dealPrice();
        holder.tv_name.setText(shopBean.getTitle());
        holder.tv_press.setText(shopBean.getPress()+"");
        holder.tv_num.setText(shopBean.getSelect_num()+"");
        if (mlist.get(i).isCheck()){
            holder.tv_cb_text.setVisibility(View.GONE);
        }

        holder.cb.setChecked(mlist.get(i).isCheck());
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final boolean nowBeanChecked = shopBean.isCheck();
                // mlist.get(i).setCheck(isChecked);
                //onChangeMlistListener.onChangeMlist(mlist);
                shopBean.setCheck(!nowBeanChecked);
                notifyDataSetChanged();
                dealPrice();
            }
        });

        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TextView textView= (TextView) v.getRootView().findViewById(R.id.id_tv_num);
                dealAdd(shopBean);
            }
        });
        holder.iv_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TextView textView= (TextView) v.getRootView().findViewById(R.id.id_tv_num);
                dealReduce(shopBean);
            }
        });

        onAllCheckedBoxNeedChangeListener.onCheckedBoxNeedChange(dealAllIsChecked());

        //删除商品
        holder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  Toast.makeText(context, "删除商品：" + goodsBean.getName(), Toast.LENGTH_SHORT).show();
                removeOneGood(i);
            }
        });

        return view;
    }
    static class ViewHolder{
        CheckBox cb;
        TextView tv_cb_text;
        ImageView iv_delete;
        ImageView iv_icon;
        TextView tv_name;
        TextView tv_press;
        ImageView iv_reduce;
        TextView tv_num;
        ImageView iv_add;
    }

    //价格计算
    public void dealPrice() {
        // showList();
        totalCount = 0;
        totalPrice = 0.00;
        for (int i = 0; i < mlist.size(); i++) {
            //StoreBean storeBean= (StoreBean) parentMapList.get(i).get("parentName");

            ShoppingBean shoppingBean = mlist.get(i);

                double discountPrice = shoppingBean.getPress();
                int count = shoppingBean.getSelect_num();
                if (shoppingBean.isCheck()) {
                    totalCount++;//单品多数量只记1
                    totalPrice += discountPrice * count;
                }
        }
        //计算回调
        onGoodsCheckedChangeListener.onGoodsCheckedChange(totalCount, totalPrice,mlist);
    }



    public void dealAdd(ShoppingBean shopBean) {
        int count = shopBean.getSelect_num();
        if (count == shopBean.getMax_num()) {
            Toast.makeText(context, "库存仅有"+shopBean.getMax_num(), Toast.LENGTH_SHORT).show();
            return;
        }
        count++;
        shopBean.setSelect_num(count);
        //  textView.setText(String.valueOf(count));
        notifyDataSetChanged();
        dealPrice();
    }

    public void dealReduce(ShoppingBean shopBean) {
        int count = shopBean.getSelect_num();
        if (count == 1) {
            Toast.makeText(context, "请不要再点了。。。", Toast.LENGTH_SHORT).show();
            return;
        }
        count--;
        shopBean.setSelect_num(count);
        // textView.setText(String.valueOf(count));
        notifyDataSetChanged();
        dealPrice();
    }

    //供全选按钮调用
    public void setupAllChecked(boolean isChecked) {

        for (int i = 0; i < mlist.size(); i++) {
           mlist.get(i).setCheck(isChecked);
        }
        notifyDataSetChanged();
        dealPrice();
    }

    public boolean dealAllIsChecked() {
        for (int i = 0; i < mlist.size(); i++) {
            if (!mlist.get(i).isCheck()) {
                return false;//如果有一个没选择  就false
            }
        }
        return true;

    }

    public void removeOneGood(int position) {
        mlist.remove(position);
        notifyDataSetChanged();
        dealPrice();
    }
    public void removeSelect() {
        for (int m=0;m<mlist.size();m++){
            if (mlist.get(m).isCheck()){
                mlist.remove(m);
            }
            for (int n=mlist.size()-1;n>=0;n--){
                if (mlist.get(n).isCheck()) {
                    mlist.remove(n);
                }
            }
        }
        notifyDataSetChanged();
        dealPrice();
    }


    public interface OnGoodsCheckedChangeListener {
        void onGoodsCheckedChange(int totalCount, double totalPrice,List<ShoppingBean>  newShoppingBeanList);
    }
    public interface OnAllCheckedBoxNeedChangeListener {
        void onCheckedBoxNeedChange(boolean allParentIsChecked);
    }

}
